﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Entities.PBKT;
using Entities.Admin;
using BLL.Admin;

namespace WindowsForms.Admin
{
    public partial class FormDoiTuyen : Form
    {
        public static int magiaidau;
        string  mactXS;
        public FormDoiTuyen()
        {
            InitializeComponent();
 
            dataGridViewct.AutoGenerateColumns = false;
            dataGridViewkq.AutoGenerateColumns = false;
            dataGridViewGD.AutoGenerateColumns = false;
        }

        private void FormDoiTuyen_Load(object sender, EventArgs e)
        {
            dataGridViewGD.DataSource = GiaiDau.Objects.All().OrderByDescending(x => x.ThoiGianBatDau).Skip(0).Take(3).ToList();
   
        }
        private void LoadCombobox(List<DoiTuyen> Ds)
        {
            cbbdoituyen.Items.Clear();
            foreach (var item in Ds)
                cbbdoituyen.Items.Add(item);
            cbbdoituyen.DisplayMember = "TenDT";
            cbbdoituyen.ValueMember = "MaDT";
            cbbdoituyen.Items.Insert(0, "-- Tên Đội Tuyển --");
            cbbdoituyen.SelectedIndex = 0;

        }

        private void cbbdoituyen_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbbdoituyen.SelectedIndex > 0)
            {
                string madoituyen = (cbbdoituyen.SelectedItem as DoiTuyen).MaDT;
                List<CauThu> dsct = CauThu.Objects.Find(p => p.MaDT == madoituyen).ToList();
                if (dsct.Count > 0)
                {
                    LoadCauThu(dsct);
                }
            }

        }
        private void LoadCauThu(List<CauThu> Ds)
        {
            dataGridViewct.DataSource = Ds;
        }

        

        private void dataGridViewct_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                if (e.ColumnIndex == 3)
                {
                    
                    dataGridViewkq.Rows[0].Cells[4].Value = dataGridViewct[1, e.RowIndex].Value;
                    mactXS = Convert.ToString(dataGridViewct[0, e.RowIndex].Value);
                }
                if (e.ColumnIndex == 4)
                {
                    dataGridViewkq.Rows[0].Cells[5].Value = dataGridViewct[1, e.RowIndex].Value;
                    mactXS = Convert.ToString(dataGridViewct[0, e.RowIndex].Value);

                }
                if (e.ColumnIndex == 5)
                {
                    dataGridViewkq.Rows[0].Cells[6].Value = dataGridViewct[1, e.RowIndex].Value;
                    mactXS = Convert.ToString(dataGridViewct[0, e.RowIndex].Value);
                }
            }
            catch (Exception)
            {
                
                throw;
            }
 
          
        }

        private void btnluu_Click(object sender, EventArgs e)
        {
            KetQuaGiaiDau kqgd = LayControl();
            List<KetQuaGiaiDau> Ds = KetQuaGiaiDau.Objects.All().ToList();
            if (!QuanLiGD.KiemTra(magiaidau, Ds))
            {

                if (KetQuaGiaiDau.Objects.Add(kqgd))
                {
                    MessageBox.Show("Thêm Thành Công");
                }
                else MessageBox.Show("Thêm Thất Bại");
            }
            else
            {
                DialogResult rs = MessageBox.Show("Kết Quả Giải Đấu Đã Có Bạn Muốn Cập Nhật Không", "Thông Báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (rs == DialogResult.Yes)
                {
                    if (KetQuaGiaiDau.Objects.Update(kqgd))
                        MessageBox.Show("Cập Nhật Thành Công");
                    else MessageBox.Show("Cập Nhật Thất Bại");
                }
            }
        }
        private KetQuaGiaiDau LayControl()
        {
            
                
            KetQuaGiaiDau kq = new KetQuaGiaiDau();
            kq.MaGiaiDau = magiaidau;
            kq.DoiVoDich=Convert.ToString( dataGridViewkq[1,0].Value);
                kq.DoiVeNhi=Convert.ToString( dataGridViewkq[2,0].Value);
                kq.DoiVeBa=Convert.ToString(dataGridViewkq[3, 0].Value);
            kq.CauThuXS=Convert.ToString(dataGridViewkq[4, 0].Value);
                kq.ThuMonXS=Convert.ToString(dataGridViewkq[5, 0].Value);
                kq.VuaPhaLuoi=Convert.ToString(dataGridViewkq[6, 0].Value);
                kq.GhiChu = Convert.ToString(dataGridViewkq[7, 0].Value);
                return kq;
        }

        private void btnthoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void dataGridViewGD_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == 3)
            {
                magiaidau = Convert.ToInt32(dataGridViewGD[0, e.RowIndex].Value);
                GiaiDau gd = GiaiDau.Objects.GetById(magiaidau);
                if (gd != null)
                //dataGridViewkq.Rows.Add(1);
                {
                    dataGridViewkq.Rows.Clear();
                    dataGridViewkq.Rows.Add(gd.TenGiaiDau, "", "", "", "", "", "");
                }

                List<DoiTuyen> dt = DoiTuyen.Objects.Find(p => p.MaGD == magiaidau).ToList();
                if (dt.Count > 0)
                {
                    LoadCombobox(dt);


                }
                else cbbdoituyen.Items.Clear();
                dataGridViewct.DataSource = null;

            }
        }
    }
}
